/*
The goal of this code is to construct an appropriate NMDB sample, to compare the
ASMB sample to.  This is useful for Table B.2
*/


* I got these dates from an email from Bob Avery on 2/16/2023 
local month_1_2016 204 // December 2014
local month_2_2016 207 // March 2015

local month_1_2017 216 // December 2015
local month_2_2017 219 // March 2016

local month_1_2018 219 // March 2016
local month_2_2018 228 // December 2016

/**********************************************************************************************
Step 1: Create one dataset of comparable NMDB defaulters for each year
**********************************************************************************************/

foreach yy in 2016 2017 2018   {

use "$NMDB/nmdb_20210113_v130_month1.dta", clear

drop if inlist(perfm`month_1_`yy'',"K","M","P","Q","S","A","7","8","9") // drop loans in bankruptcy, foreclosure, or not active as of first date
drop if inlist(perfm`month_2_`yy'',"K","M","P","Q","S","A","7","8","9") // drop loans in bankruptcy, foreclosure, or not active as of second date

gen open_year = floor(open_dt / 10000)

* Label, but not yet drop, newly-opened loans.  We can drop them later and want to investigate before we do.
gen new_loan = (open_year>=(`yy'-2)) & !mi(open_year) // E.g. the 2017 survey only included loans opened 2014 or earlier


merge 1:1 loannb using  "$NMDB/nmdb_20210113_v130_loans.dta", keepusing(age_1 arm borrowers dti ecoa_cd ethnic_1 gender_1 loan_type med_income_hmda_2019 minpop_tract open_dt score_orig_1 race_1 term income loan_amt value hpiq* interest ltv pct_hispanic_tract pct_black_tract pct_minority_tract cltv_all cltv_closed delq* rucc2013 servicer_size_flag balq* close2ndq* open2ndq* open_month open_year geo*) keep(match) nogen

merge 1:1 loannb using "$NMDB/nmdb_20210113_v130_month2.dta", keepusing(gridm*)  keep(match) nogen


gen survey_year = `yy' //survey_year is expected by define_defaulters.do

do "$DOFILES/9999_define_defaulters.do"

gen in_default_sample = defaulter==1 & !mi(last_current_month_b4_default)

save "$DATA_OUT/processing/NMDB_comparison_sample_`yy'", replace



}






/**********************************************************************************************
Step 2: Append the 2015-2017 datasets to save the dataset for comparison.
Note many mortgages will be in there multiple times.
**********************************************************************************************/

use "$DATA_OUT/processing/NMDB_comparison_sample_2016"
append using "$DATA_OUT/processing/NMDB_comparison_sample_2017"
append using "$DATA_OUT/processing/NMDB_comparison_sample_2018"


/**********************************************************************************************
Create proxies using NMDB data
**********************************************************************************************/


** Also use the quarterly information to create the 60D proxy for liquidity shocks
do "$DOFILES/9999_create_60D_proxy_for_liquidity_shocks.do"

** Also use the quarterly information to create the MtM home equity proxies
global BKfine_bool = 0 // without ASMB responses we have no condo information so we cannot create the fine BK proxy
do "$DOFILES/9999_create_MtM_equity_proxies.do"


save "$DATA_OUT/NMDB_sample_for_ASMB_comparison", replace









